Skip to content

v4.2.0: Grace for degradation!

Compare
Choose a tag to compare
@bczsalba bczsalba released this 21 Mar 18:35
· 525 commits to master since this release

This version adds graceful color degradation, which (to my knowledge), makes us one of the first terminal libraries to do so, but definitely the first in our specific niche!

This is all done automatically, using the newly-rehomed Terminal class and its instance. Essentially, every time a color is created through TIM, the new str_to_color method is called, which will by default localize all output colors. During this localization the xterm-standard HEX value for the given color is found, and that is then used to create a new color in the terminal's color space.

For example, lets say you have an RGB color of 000;111;222, but your terminal only supports the xterm-256 palette. PyTermGUI will, under the hood, determine the RGB value of this color, and get the weighted difference between it and and all known palette colors. Once that is done it will find the closest match, create an IndexedColor with the newly found index and return that back to TIM.

Now this all sounds very computationally complicated, which it is. Thus, there are a couple of simple "caches" inside the new colors module. The first one caches all input given to str_to_color with its corresponding output, and the second one saves all input and output of the aforementioned color matching. Because of this, my machine shows only a sub-1 ms difference of frametime parsing a true-color and 16-color representation of the same, complicated markup frame (namely, ptg -m).

Additionally, there is now a really robust implementation of the https://no-color.org standard! Unlike pretty much all modules, we don't just completely omit colors, rather we calculate each color's brightness and then assign a value to them in the xterm-256 greyscale range, thereby retaining most of the contrast, the ability to set background colors and more. You can see some demos of this further down the page.

Additions

  • Add a graceful degradation Color layer (5053603)
  • Expose color support in terminal (f265807)
  • Add support for prettifying UserDict and UserList (35e789c)
  • Add support for NO_COLOR using greyscale ramps (0cdf712)
  • Add colors module with color types for all terminal colors (f265807)

Refactors

  • Move terminal to its own module (5aeb397)

Bugfixes

  • Fix [ sometimes being ommitted during prettify_markup (a2defd8)

Showcase

The same HSL to RGB color palette printed by TIM, with only terminal.forced_colorspace being changed between runs.

Screenshot 2022-03-21 at 18 17 36

ptg ran with NO_COLOR=1 as an environment variable.

Screenshot 2022-03-21 at 19 32 54